package com.qf.web;

import com.github.pagehelper.PageInfo;
import com.qf.bean.Dept;
import com.qf.bean.Menu;
import com.qf.bean.Role;
import com.qf.service.DeptService;
import com.qf.service.MenuService;
import com.qf.service.RoleService;
import com.qf.util.Enume1.PageEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * 2019/11/21
 * 键盘敲烂，月薪过万
 * 登高必自卑，行远必自迩。
 * 不忘初心：像初学者一样拥有一颗谦虚、开放的心态。
 * 李学峰
 */
@Controller
public class RoleController {
    @Autowired
   private RoleService roleService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private MenuService menuService;


    //更新角色
    @RequestMapping("/resource/demo3/findbyroleid")
    public String findbyid(int rid,ModelMap map){
        //查询角色信息
        Role role = roleService.selectByPrimaryKey(rid);
        map.addAttribute("role",role);
        //查询所有部门信息
        List<Dept> depts = deptService.findalldepts();
        map.addAttribute("depts",depts);

        List<Menu> menus = menuService.findallMenus();
        map.addAttribute("menus",menus);

        return "/resource/demo3/update";
    }



    //新增角色
    @RequestMapping("/resource/demo3/addrole")
    public String addrole(Role role,int[] mid){
        int insert = roleService.insert(role,mid);
        return "redirect:/resource/demo3/getmenus";

    }




//查询所有的部门
@RequestMapping("/resource/demo3/getdepts")
public String getdepts(ModelMap map){
    List<Dept> deptLists= deptService.findalldepts();
    map.put("depts",deptLists);
    //查询所有的菜单列表
    List<Menu> menus = menuService.findallMenus();
    map.put("menus",menus);
    return "/resource/demo3/add";

}
    @RequestMapping("/getroles")
//其作用是将方法的返回值以特定的格式写入到response的body区域，进而将数据返回给客户端


@ResponseBody
        //根据部门ID查询列表   did和jajx的参数一样
    public  List<Role> getbydeptid(int did){
    List<Role> list = roleService.findbyDeptid(did);
    return list;
}
    //全查+分页:参数都有->pageindex(设置默认值),模糊查询的值,Modelmap
    @RequestMapping("/resource/demo3/getmenus")
    public String getmenus(@RequestParam(defaultValue = "1") int pageindex, String title,
                           String deptname, @RequestParam(defaultValue = "-1") int status,
                           ModelMap map){
        //1.调取serevice方法，返回值必须是PageInfo,2.使用modelMap存模糊查询的条件值(做数据回显)
        //1.调取service方法
        PageInfo<Menu> pageInfo = roleService.findmenus(pageindex, PageEnum.pagesize.getSize(), title, deptname, status);
        map.addAttribute("pi",pageInfo);
        map.addAttribute("deptname",deptname);
        map.addAttribute("title",title);
        return "resource/demo3/list";
    }

    //lxf删除/resource/demo3/deleteid
    @RequestMapping("/resource/demo3/deleteid")
    public  void deletebyid(Integer roleid, HttpServletResponse response){
        int i = roleService.deleteByPrimaryKey(roleid);
        try {
            response.setContentType("text/html;charset=utf-8");
            PrintWriter writer = response.getWriter();
            if (i>0){
                writer.print("<script>alert('删除成功');location.href='/resource/demo3/getmenus'</script>'");
            }else {
                writer.print("<script>alert('删除失败');location.href='/resource/demo3/getmenus'</script>'");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}










